var express = require('express');
var router = express.Router();
let crypto = require("crypto");
let userService = require("../service/userService");
let jwt = require("jsonwebtoken");


/* GET users listing. */
router.get('/', function(req, res, next) {
    res.send('respond with a resource');
});
//查询所有测试人员的接口
router.get("/testuser", async(req, res, next) => {
    let list = await userService.query({ type: 2 })
    list = list.map((item) => item.username)
    res.json({
        code: 1,
        msg: "success",
        list
    })
})


//查询所有开发人员的接口
router.get("/programuser", async(req, res, next) => {
    let list = await userService.query({ type: 3 })
    list = list.map((item) => item.username)
    res.json({
        code: 1,
        msg: "success",
        list
    })
})

router.post("/searchuser", async(req, res, next) => {
    let info = req.body
    let list = await userService.query(info)
    if (list.length) {
        if (list[0].type == 1) {
            res.json({
                code: 0,
                msg: "用户无权限查看"
            })
        } else {
            res.json({
                code: 1,
                msg: "success",
                list
            })
        }

    } else {
        res.json({
            code: 0,
            msg: "用户不存在"
        })
    }

})

router.post("/deluser", (req, res, next) => {
    let token = req.headers.token;
    jwt.verify(token, "abc", async(err, info) => {
        if (err) {
            res.json({
                code: 0,
                msg: "token解析异常，请重新登录"
            })
        } else {
            if (info.type == 1) {
                let { _id } = req.body;
                console.log(_id);

                let list = await userService.deluser({ _id });

                res.json({
                    code: 1,
                    msg: "删除成功",
                    list
                })
            } else {
                res.json({
                    code: 0,
                    msg: "用户无权限进行此操作"
                })
            }
        }
    })
})
router.get("/userlist", (req, res, next) => {
    let token = req.headers.token;
    jwt.verify(token, "abc", async(err, info) => {
        if (err) {
            res.json({
                code: 0,
                msg: "token解析异常，请重新登录"
            })
        } else {
            if (info.type == 1) {
                let { key, shownum = 5, page, type } = req.query;
                let skipnum = (page - 1) * shownum;
                let params = {};
                let reg = new RegExp(key);
                params.username = reg;
                if (type) {
                    params.type = type * 1;
                }

                let list = await userService.queryall(params, skipnum, shownum * 1);
                let count = await userService.count(params);
                if (list.length) {
                    res.json({
                        code: 1,
                        msg: "查询成功",
                        list,
                        count
                    })
                } else {
                    res.json({
                        code: 0,
                        msg: "查询数据为空"
                    })
                }


                // res.json({
                //     code: 1,
                //     params,
                //     skipnum,
                //     shownum
                // })


                // let list = await userService.query();
                // if (list.length) {
                //     res.json({
                //         code: 1,
                //         msg: "查询成功",
                //         list
                //     })
                // } else {
                //     res.json({
                //         code: 0,
                //         msg: "查询数据为空"
                //     })
                // }

            } else {
                res.json({
                    code: 0,
                    msg: "查询用户权限不够"
                })
            }
        }
    })

})
router.post("/login", async(req, res, next) => {
    let { username, userpwd } = req.body;
    let md5 = crypto.createHash("md5");
    userpwd = md5.update(userpwd).digest("hex");
    let list = await userService.query({ username, userpwd });
    if (list.length) {

        let token = jwt.sign({ username, userid: list[0]._id, type: list[0].type },
            "abc", { expiresIn: 60 * 60 });
        // console.log(token);
        let path = "program";
        if (list[0].type == 1) {
            path = "manager"
        } else if (list[0].type == 2) {
            path = "test"
        } else if (list[0].type == 3) {
            path = "program"
        }

        res.json({
            code: 1,
            msg: "用户登录成功",
            token,
            path,
        })
    } else {
        res.json({
            code: 0,
            msg: "用户名或密码错误"
        })
    }
    // res.json({
    //     code: 1,
    //     msg: "success"
    // })

})
router.post("/register", async(req, res, next) => {

    let token = req.headers.token;
    jwt.verify(token, "abc", async(err, info) => {
        if (err) {
            res.json({
                code: 0,
                msg: "token解析异常，请重新登录"
            })
        } else {
            if (info.type == 1) {
                let { username, userpwd = "123456", usertype = "G1", type = 3 } = req.body;
                if (type == 1) {
                    res.json({
                        code: 0,
                        msg: "项目经理有且仅有一位，无法通过此系统注册"
                    })
                } else {
                    let md5 = crypto.createHash("md5");
                    userpwd = md5.update(userpwd).digest("hex");
                    let list = await userService.query({ username });
                    if (list.length) {
                        res.json({
                            code: 0,
                            msg: "用户名已存在",
                        })
                    } else {
                        if (username) {
                            await userService.insert({ username, userpwd, usertype, type });
                            res.json({
                                code: 1,
                                msg: "用户注册并且添加成功"
                            })
                        } else {
                            res.json({
                                code: 0,
                                msg: "用户名不合规"
                            })
                        }

                    }
                }

            } else {
                res.json({
                    code: 0,
                    msg: "用户权限不够，无权限注册员工"
                })
            }
        }
    })

})

module.exports = router;